// Package crons
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2023 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
package crons

import (
	"context"
	"encoding/json"
	"fmt"
	"hotgo/api/admin/stockblock"
	"hotgo/internal/library/cron"
	"hotgo/internal/model/entity"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/client"
	"hotgo/utility/tradeDate"
	"io"
	"slices"
	"time"
)

func init() {
	cron.Register(StockBlockDetail)
}

// StockBlockDetail 测试任务（无参数）
var StockBlockDetail = &cStockBlockDetail{name: "block_detail"}
var StockBlockDetailUrl = "https://apphq.longhuvip.com/w1/api/index.php?st=30&Index=0&old=1&IsKZZType=0&Order=1&PhoneOSNew=1&Token=4e7fa8458a2add3f14a50ca79e863772&a=ZhiShuStockList_W8&UserID=1973778&apiv=w31&Type=6&c=ZhiShuRanking&VerSion=5.7.0.12&DeviceID=00000000-296c-20ad-0000-00003eb74e84&IsZZ=0&PlateID="

type StockBlockDetailResp struct {
	ErrCode string                 `json:"errcode"`
	List    []StockBlockDetailItem `json:"list"`
}
type StockBlockDetailItem []any

type cStockBlockDetail struct {
	name string
}

func (c *cStockBlockDetail) GetName() string {
	return c.name
}

// Execute 执行任务
func (c *cStockBlockDetail) Execute(ctx context.Context, parser *cron.Parser) (err error) {
	var req = new(stockblock.ListReq)
	req.StockBlockListInp.Date = tradeDate.GetLatestDay()
	list, _, err := service.SysStockBlock().List(ctx, &req.StockBlockListInp)
	if err != nil {
		return
	}
	var platCodes []string
	for _, listModel := range list {
		if listModel.Date == req.Date {
			code := listModel.Code
			if !slices.Contains(platCodes, code) {
				platCodes = append(platCodes, code)
			}
		}
	}

	for _, platCode := range platCodes {
		time.Sleep(1 * time.Second)
		httpClient := client.GetClient()
		response, err := httpClient.Get(ctx, fmt.Sprintf("%s%s", StockBlockDetailUrl, platCode))
		if err != nil {
			parser.Logger.Warning(ctx, "cron CloseOrder Execute http.Get err:%+v", err)
			continue
		}
		defer response.Body.Close()
		body, err := io.ReadAll(response.Body)
		if err != nil {
			parser.Logger.Warning(ctx, "cron CloseOrder Execute io.ReadAll err:%+v", err)
			continue
		}
		var resp StockBlockDetailResp
		err = json.Unmarshal(body, &resp)
		if err != nil {
			parser.Logger.Warning(ctx, "cron CloseOrder Execute json.Unmarshal err:%+v", err)
			continue
		}
		if resp.ErrCode == "0" {
			for _, emResp := range resp.List {
				//  	"301361",
				//      "众智科技",
				//      "",
				//      0,
				//      "虚拟电厂、智能电网",
				//      26.71,
				//      19.99,
				//      507502847,
				//      66.63,
				//      0,
				//      776833640,
				//      239656252,
				//      -205874960,
				//      33781292,
				//      47.22,
				//      40.57,
				//      6.66,
				//      30.85,
				//      26.5,
				//      4.35,
				//      11.76,
				//      13.75,
				//      0,
				//      "2连板",
				//      "龙一",
				//      66.63,
				//      "",
				//      0,
				//      35588028,
				//      119451048,
				//      "",
				//      "",
				//      "",
				//      0,
				//      0,
				//      0,
				//      0,
				//      3107334560,
				//      776833640,
				//      "虚拟电厂",
				//      1,
				//      1,
				//      0,
				//      0,
				//      0,
				//      0,
				//      0,
				//      0,
				//      0,
				//      "2022-03-31",
				//      -4714769,
				//      0,
				//      0,
				//      2.84,
				//      19.99,
				//      0,
				//      26.71,
				//      "19.99",
				//      9686,
				//      5
				code, _ := emResp[0].(string)
				subBlockName, _ := emResp[4].(string)
				strength, _ := emResp[23].(string)
				position, _ := emResp[24].(string)
				err := service.SysStockBlockDetail().Edit(ctx, &sysin.StockBlockDetailEditInp{
					StockBlockDetail: entity.StockBlockDetail{
						Code:         code,
						BlockCode:    platCode,
						SubBlockName: subBlockName,
						Position:     position,
						Strength:     strength,
						Date:         req.StockBlockListInp.Date,
					},
				})
				if err != nil {
					parser.Logger.Warning(ctx, "cron CloseOrder Execute service.SysStockZhASpotEm().Edit err:%+v", err)
					continue
				}
			}
		}
	}

	return
}
